import re

from playwright.sync_api import sync_playwright
from pjstealth import stealth_sync

from utils import RedisHelper


class Helper:
    def __init__(self):
        self.ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.5504.116 Safari/537.36 Edg/101.0.1210.47"
        self.proxies = None
        self.redis_client = RedisHelper().get_conn()


    def main(self):
        with sync_playwright() as p:
            browser = p.chromium.launch(headless=True, proxy=self.proxies if self.proxies else None)
            context = browser.new_context(user_agent=self.ua, accept_downloads=True)
            page = context.new_page()
            stealth_sync(page)
            page.goto("https://store.gx.me/")
            page.wait_for_load_state('networkidle')
            html = page.content()
            page.close()
            context.close()
            browser.close()
            return html

    def get_url(self, html_str):
        url_list = re.findall('<a href="(.*?)">', html_str)
        for url in url_list:
            print(url)
            self.redis_client.sadd("GX_url", url)



if __name__ == '__main__':
    html_str = Helper().main()
    Helper().get_url(html_str)